/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.tags;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.BeanFactory;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  获取配置文件变量参数标签
 *
 * @author Hou Dayu 创建时间：2016-8-22
 */
@SuppressWarnings("serial")
public class PortalParamsTag extends TagSupport {

    private String varKey;

    private String varFileName;

    private String sqlQuery;//查询条件

    //private JspWriter writer;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public int doStartTag() throws JspException {
        boolean b = false;
        try {
//            if ("tokenId".equals(varKey)) {//隐示同步登录信息
//                HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
//                String tokenId = String.valueOf(request.getSession().getAttribute("tokenId"));
//                if (StrUtil.isNotNull(tokenId)) {
//                    String ssoUrl =
//                            ConfigUtil.getInstance().getValueByKey("uct_server") + "?tokenId="
//                                    + tokenId;
//                    String longPage =
//                            "<iframe frameborder='0' marginheight='0' marginwidth='0' allowtransparency='true' src='"
//                                    + ssoUrl
//                                    + "' width='0' height='0' noresize='noresize' scrolling='No'/>";
//                    JspWriter writer = this.pageContext.getOut();// 返回一个JspWriter
//                    writer.print(longPage);
//                    b = true;
//                    writer.flush();
//                    request.getSession().removeAttribute("tokenId");
//                }
//            } else if ("loginSys".equals(varKey)) {//获取当前登录用户授权的系统
//                HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
//                Map params = new HashMap<String, String>();
//                json2map1(params, request);
//                params.put("doCode", "dymicSql");
//                params.put("resultType",
//                        StrUtil.doNull(request.getParameter("resultType"), "objInfo"));
//                params.put("resultObjType", "list");
//                params.put("modelTypes", "s_uctUserSys");
//                params.put("sysPass", ConfigUtil.getInstance().interfacePass());
//                ResultEntity entity = (ResultEntity) ((PortalPlugin) BeanFactory.getInstance()
//                        .getBeans("net")).doInvoke(request, null, params);
//                String sysNo = "";
//                List<Map> list = null;
//                if (entity.getEntity() instanceof Page) {
//                    Page page = (Page) entity.getEntity();
//                    list = page.getPageObjects();
//                } else {
//                    list = (List) entity.getEntity();
//                }
//                if (!Validator.isEmpty(list)) {
//                    for (Map map : list) {
//                        sysNo += "/" + map.get("sysNo") + ",";
//                    }
//                }
//                request.setAttribute("sysNos", sysNo);
//            } else {
                String value = "";
                if (StrUtil.isNull(varFileName)) {
                    varFileName = "unity_config";
                }
                value = ConfigUtil.getInstance()
                        .getValueByProperty("config/" + varFileName + ".properties", varKey);
                if (StrUtil.isNotNull(value)) {
                    JspWriter writer = this.pageContext.getOut();// 返回一个JspWriter
                    writer.print(value);
                    b = true;
                    writer.flush();
                }
//            }
        } catch (IOException e) {
        }
        if (b) {
            return super.EVAL_BODY_INCLUDE;
        } else {
            return super.SKIP_BODY;
        }
    }

    /**
     * json转map
     */
    private void json2map1(Map<String, Object> params, HttpServletRequest request) {
        if (StrUtil.isNotNull(sqlQuery)) {
            JSONObject jsonObject = JSONObject.fromObject(sqlQuery);
            Map<String, Object> map1 = JSONObject.fromObject(jsonObject);
            params.putAll(map1);
        }
    }

    //	public int doEndTag() {
    //		try {
    //			if (null != writer)
    //				writer.close();
    //		} catch (Exception e) {
    //		}
    //		return super.EVAL_BODY_INCLUDE;
    //	}

    public String getVarKey() {
        return varKey;
    }

    public void setVarKey(String varKey) {
        this.varKey = varKey;
    }

    public String getVarFileName() {
        return varFileName;
    }

    public void setVarFileName(String varFileName) {
        this.varFileName = varFileName;
    }

    public String getSqlQuery() {
        return sqlQuery;
    }

    public void setSqlQuery(String sqlQuery) {
        this.sqlQuery = sqlQuery;
    }

}
